package edu.ustb.efws.base.service;

import edu.ustb.efws.BaseException;
import edu.ustb.efws.base.domain.Alarm;
import edu.ustb.efws.base.mapper.AlarmMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 报警业务服务
 *
 * @author TS Group
 */
@Service
@Transactional(readOnly = true)
public class AlarmService {
    private final AlarmMapper mapper;

    @Autowired
    public AlarmService(AlarmMapper mapper) {
        this.mapper = mapper;
    }

    /**
     * 新增报警设置
     *
     * @param t {@link Alarm}
     * @return {@link Alarm}
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public Alarm save(Alarm t){

        Date now = new Date();
        t.setCreateTime(now);
        t.setUpdateTime(now);
        if(t.getPositions() == null){
            t.setPositions(new ArrayList<>());
        }

        if(mapper.insert(t) == 0){
            throw new BaseException("新增报警设置失败");
        }

        return t;
    }

    /**
     * 修改报警设置
     *
     * @param t {@link Alarm}
     * @return {@link Alarm}
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public Alarm update(Alarm t){
        if(t.getPositions() == null){
            t.setPositions(new ArrayList<>());
        }
        if(mapper.update(t) == 0){
            throw new BaseException("修改报警设置失败");
        }
        return mapper.findOne(t.getId());
    }

    /**
     * 开启报警
     *
     * @param id 编号
     * @return {@link Alarm}
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public Alarm open(long id){
        if(mapper.updateEnable(id, true) == 0){
            throw new BaseException("开启报警失败");
        }
        return mapper.findOne(id);
    }

    /**
     * 关闭报警
     *
     * @param id 编号
     * @return {@link Alarm}
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public Alarm close(long id){
        if(mapper.updateEnable(id, false) == 0){
            throw new BaseException("关闭报警失败");
        }
        return mapper.findOne(id);
    }

    /**
     * 删除报警
     *
     * @param id 编号
     * @return true 成功
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean delete(long id){
        return mapper.delete(id) > 0;
    }

    /**
     * 获取报警设置
     *
     * @param id 编号
     * @return {@link Alarm}
     */
    public Alarm get(long id){
        Alarm t = mapper.findOne(id);
        if(t == null){
            throw new BaseException("报警设置不存在");
        }
        return t;
    }

    /**
     * 查询所有报警设置
     *
     * @return 报警设置集合
     */
    public List<Alarm> findAll(){
        return mapper.findAll();
    }

    /**
     * 查询记录数
     *
     * @param name 姓名
     * @return 记录数
     */
    public long count(String name){
        return mapper.count(name);
    }

    /**
     * 查询报警设置
     *
     * @param name 姓名
     * @param offset 记录开始位置
     * @param limit 记录数
     * @return 报警集合
     */
    public List<Alarm> query(String name, int offset, int limit){
        return mapper.find(name, offset, limit);
    }
}
